/**
 * 
 */
package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.pojo.Student;

/**
 * 山东
 * 排名:完成
 * 去年排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-19
 */
public class Shandong extends Exam
{
	public static void main(String[] args)
	{
		Student student = new Student("15370687110006", "622826199305271916", "370682199512090022", "15622826152803",
				"");
		Shandong gansu = new Shandong(student);
		System.out.println(gansu.login(""));
	}

	public Shandong(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://cx.sdzk.cn/gkcj/gkcjAct.jsp", mSha1);
		loginRequest.setCharSet("gbk");
		Document paraDocument = Jsoup.parse(curlPost(loginRequest));
		loginRequest.addHeader("Referer", "http://cx.sdzk.cn/gkcj/gkcjAct.jsp");
		loginRequest.addParams("iptKsh", mStudent.getStudentId());
		loginRequest.addParams("iptSfzh", mStudent.getIdCard());
		loginRequest.addParams("iptYzm", paraDocument.select("input[name=iptYzm]").val());
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("夏季高考成绩"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("考生不存在"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		Elements trsif = loginDocument.select("table").get(0).select("tr");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trsif.get(1).select("td").get(1).text());
		result.put("ranking", trsif.get(1).select("td").get(8).text());
		result.put("province", "山东省");
		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trsif.get(1).select("td").get(2).text());
		scoreObject.put("数学", trsif.get(1).select("td").get(3).text());
		scoreObject.put("外语", trsif.get(1).select("td").get(4).text());
		scoreObject.put("综合", trsif.get(1).select("td").get(5).text());
		scoreObject.put("总分", trsif.get(1).select("td").get(6).text());
		scoreObject.put("加分", trsif.get(1).select("td").get(7).text());

		result.put("score", scoreObject.toString());
		return result.toString();
	}
}
